﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI.WebControls;
using BLL;
using Model;
using System.Text.RegularExpressions;

public partial class InstrumentManage_ENAManage : System.Web.UI.Page
{
    private const string valEx = @"^((?:(?:25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d)))\.){3}(?:25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d))))$";

    private string var_ENA
    {
        get
        {
            return ViewState["var_ENA"].ToString();
        }
        set
        {
            ViewState["var_ENA"] = value;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GridView2.DataSource = null;
            GridView2.DataBind();
            BindENAs();            
        }
        if (HttpContext.Current != null)
        {
            if (Roles.IsUserInRole("Guest"))
            {
                this.Page.Unload += new EventHandler(Page_Unload);
                this.Button1.Click += new EventHandler(Guest);
                this.Button1.Click -= new EventHandler(Button1_Click);
                this.GridView1.RowEditing += new GridViewEditEventHandler(Guest);
                this.GridView1.RowEditing -= new GridViewEditEventHandler(GridView1_RowEditing);
                this.GridView1.RowDeleting += new GridViewDeleteEventHandler(Guest);
                this.GridView1.RowDeleting -= new GridViewDeleteEventHandler(GridView1_RowDeleting);
                this.GridView2.RowEditing += new GridViewEditEventHandler(Guest);
                this.GridView2.RowEditing -= new GridViewEditEventHandler(GridView2_RowEditing);
                this.GridView2.RowDeleting += new GridViewDeleteEventHandler(Guest);
                this.GridView2.RowDeleting -= new GridViewDeleteEventHandler(GridView2_RowDeleting);
            }
        }
    }

    void Page_Unload(object sender, EventArgs e)
    {
        this.Page.Unload -= new EventHandler(Page_Unload);
        this.Button1.Click -= new EventHandler(Guest);
        this.Button1.Click += new EventHandler(Button1_Click);
        this.GridView1.RowEditing -= new GridViewEditEventHandler(Guest);
        this.GridView1.RowEditing += new GridViewEditEventHandler(GridView1_RowEditing);
        this.GridView1.RowDeleting -= new GridViewDeleteEventHandler(Guest);
        this.GridView1.RowDeleting += new GridViewDeleteEventHandler(GridView1_RowDeleting);
        this.GridView2.RowEditing -= new GridViewEditEventHandler(Guest);
        this.GridView2.RowEditing += new GridViewEditEventHandler(GridView2_RowEditing);
        this.GridView2.RowDeleting -= new GridViewDeleteEventHandler(Guest);
        this.GridView2.RowDeleting += new GridViewDeleteEventHandler(GridView2_RowDeleting);
    }    

    public void BindENAs()
    {
        ENA eNA = new ENA();
        this.GridView1.DataSource = eNA.LoadEntities();
        this.GridView1.DataBind();
    }


    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        BindENAs();
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        BindENAs();
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        BindENAs();
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        ENAInfo eNAInfo = new ENAInfo();
        eNAInfo.ENAID = GridView1.DataKeys[e.RowIndex].Values[0].ToString();
        eNAInfo.ENAPlace = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.Trim().ToString();
        string enaip = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.Trim().ToString();
        if (Regex.IsMatch(enaip, valEx))
        {
            eNAInfo.ENAIP = enaip;
        }
        else
        {
            string str1 = "要更新的矢网IP格式不正确，请确认！";
            ClientScript.RegisterStartupScript(ClientScript.GetType(), "", "<script>alert('" + str1 + "');</script>");
            return;
        }
        eNAInfo.CreateTIme = Convert.ToDateTime(((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.Trim().ToString());
        eNAInfo.ENAMau = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[4].Controls[0])).Text.Trim().ToString();
        eNAInfo.ENAClass = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[5].Controls[0])).Text.Trim().ToString();
        eNAInfo.ENAFunction = ((DropDownList)(GridView1.Rows[e.RowIndex].FindControl("DDLShiyebu"))).SelectedValue;
        eNAInfo.ENAKind = ((DropDownList)(GridView1.Rows[e.RowIndex].FindControl("DDLYongtubumen"))).SelectedValue;
        eNAInfo.ENAState  = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[8].Controls[0])).Text.Trim().ToString();

        ENA eNA = new ENA();
        if (string.IsNullOrEmpty(eNA.LoadEntity(eNAInfo.ENAID).ENAID))
        {
            string str1 = "要更新的矢网编号不正确，请确认！";
            ClientScript.RegisterStartupScript(ClientScript.GetType(), "", "<script>alert('" + str1 + "');</script>");
            return;
        }
        else
        {
            eNA.Update(eNAInfo);
            GridView1.EditIndex = -1;
            BindENAs();
        }
    }

    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string eNAID = GridView1.DataKeys[e.RowIndex].Values[0].ToString();
        ENA eNA = new ENA();        
        eNA.Delete(eNAID);
        BindENAs();        
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        ENA eNA = new ENA();
        ENAInfo eNAInfo = new ENAInfo(TextBox1.Text.Trim(), TextBox2.Text.Trim(), TextBox3.Text.Trim(), DateTime.Now, TextBox5.Text.Trim(), TextBox6.Text.Trim(), this.DropDownList1.SelectedValue, this.DropDownList2.SelectedValue, TextBox10.Text.Trim());
        ENAInfo eInfo = eNA.LoadEntity(TextBox1.Text.Trim());
        if (eInfo.ENAID == eNAInfo.ENAID)
        {
            string str1 = "矢网编号已存在！";
            ClientScript.RegisterStartupScript(ClientScript.GetType(), "", "<script>alert('" + str1 + "');</script>");
            return;
        }
        else
        {
            eNA.Insert(eNAInfo);
            string str1 = "添加成功！";
            ClientScript.RegisterStartupScript(ClientScript.GetType(), "", "<script>alert('" + str1 + "');</script>");
            BindENAs();

        }
    }

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (e.Row.RowState==DataControlRowState.Normal||e.Row.RowState==DataControlRowState.Alternate)
            {
                //((LinkButton)e.Row.Cells[10].Controls[0]).Attributes.Add("onclick", "javascript:return window.open('http://www.baidu.com')");
                ((LinkButton)e.Row.Cells[10].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('你确认要删除!')");
            }
        }
        if ((((DropDownList)e.Row.FindControl("DDLShiyebu")) != null) && (((DropDownList)e.Row.FindControl("DDLYongtubumen")) != null))
        {
            DropDownList ddlshiyebu = (DropDownList)e.Row.FindControl("DDLShiyebu");
            ddlshiyebu.SelectedValue = ((HiddenField)e.Row.FindControl("HDFShiyebu")).Value;
            DropDownList ddlyongtubumen = (DropDownList)e.Row.FindControl("DDLYongtubumen");
            ddlyongtubumen.SelectedValue = ((HiddenField)e.Row.FindControl("HDFYongtubumen")).Value;
        }
    }

//---------------------------------------------------------------------------------------------------------------------------

    protected void Button2_Click(object sender, EventArgs e)
    {
        var_ENA = this.TextBox9.Text.Trim();
        BindENA(var_ENA);        
    }

    private void BindENA(string var_ENA)
    {
        ENA eNA = new ENA();
        IList<ENAInfo> eNAs = new List<ENAInfo>();
        if (string.IsNullOrEmpty(eNA.LoadEntity(var_ENA).ENAID))
        {
            this.GridView2.DataSource = null;
            this.GridView2.DataBind();
        }
        else
        {
            eNAs.Add(eNA.LoadEntity(var_ENA));
            this.GridView2.DataSource = eNAs;
            this.GridView2.DataBind();
        }
    }

    protected void GridView2_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        ENAInfo eNAInfo = new ENAInfo();
        eNAInfo.ENAID = GridView2.DataKeys[e.RowIndex].Values[0].ToString();
        eNAInfo.ENAPlace = ((TextBox)(GridView2.Rows[e.RowIndex].Cells[1].Controls[0])).Text.Trim().ToString();
        string enaip = ((TextBox)(GridView2.Rows[e.RowIndex].Cells[2].Controls[0])).Text.Trim().ToString();
        if (Regex.IsMatch(enaip, valEx))
        {
            eNAInfo.ENAIP = enaip;
        }
        else
        {
            string str1 = "要更新的矢网IP格式不正确，请确认！";
            ClientScript.RegisterStartupScript(ClientScript.GetType(), "", "<script>alert('" + str1 + "');</script>");
            return;
        }
        eNAInfo.CreateTIme = Convert.ToDateTime(((TextBox)(GridView2.Rows[e.RowIndex].Cells[3].Controls[0])).Text.Trim().ToString());
        eNAInfo.ENAMau = ((TextBox)(GridView2.Rows[e.RowIndex].Cells[4].Controls[0])).Text.Trim().ToString();
        eNAInfo.ENAClass = ((TextBox)(GridView2.Rows[e.RowIndex].Cells[5].Controls[0])).Text.Trim().ToString();
        eNAInfo.ENAFunction = ((DropDownList)(GridView2.Rows[e.RowIndex].FindControl("DDLShiyebu"))).SelectedValue;
        eNAInfo.ENAKind = ((DropDownList)(GridView2.Rows[e.RowIndex].FindControl("DDLYongtubumen"))).SelectedValue;
        eNAInfo.ENAState = ((TextBox)(GridView2.Rows[e.RowIndex].Cells[8].Controls[0])).Text.Trim().ToString();

        ENA eNA = new ENA();
        if (string.IsNullOrEmpty(eNA.LoadEntity(eNAInfo.ENAID).ENAID))
        {
            string str1 = "要更新的矢网编号不正确，请确认";
            ClientScript.RegisterStartupScript(ClientScript.GetType(), "", "<script>alert('" + str1 + "');</script>");
            return;
        }
        else
        {
            eNA.Update(eNAInfo);
            GridView2.EditIndex = -1;
            BindENA(var_ENA);
            BindENAs();
        }
    }

    protected void GridView2_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView2.EditIndex = e.NewEditIndex;
        BindENA(var_ENA);
    }

    protected void GridView2_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string eNAID = GridView2.DataKeys[e.RowIndex].Values[0].ToString();
        ENA eNA = new ENA();
        eNA.Delete(eNAID);
        BindENA(var_ENA);
        BindENAs();
    }

    protected void GridView2_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView2.EditIndex = -1;
        BindENA(var_ENA);
    }

    protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
            {
                //((LinkButton)e.Row.Cells[10].Controls[0]).Attributes.Add("onclick", "javascript:return window.open('http://www.baidu.com')");
                ((LinkButton)e.Row.Cells[10].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('你确认要删除!')");
            }
        }
        // 选中 DropDownList
        if ((((DropDownList)e.Row.FindControl("DDLShiyebu")) != null) && (((DropDownList)e.Row.FindControl("DDLYongtubumen")) != null))
        {
            DropDownList ddlshiyebu = (DropDownList)e.Row.FindControl("DDLShiyebu");
            ddlshiyebu.SelectedValue = ((HiddenField)e.Row.FindControl("HDFShiyebu")).Value;
            DropDownList ddlyongtubumen = (DropDownList)e.Row.FindControl("DDLYongtubumen");
            ddlyongtubumen.SelectedValue = ((HiddenField)e.Row.FindControl("HDFYongtubumen")).Value;
        }
    }

    private void Guest(object sender, EventArgs e)
    {
        string str1 = "Sorry，您所在角色，没有权限做此操作";
        ClientScript.RegisterStartupScript(ClientScript.GetType(), "", "<script>alert('" + str1 + "');</script>");
    }
}
